﻿using FileType = MapMaker.Engine.Model.GameMaker.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MapMaker.Engine.CoreCommon;
using MapMaker.Engine.Model.VXAce;
using System.Collections;

namespace MapMaker.Engine.Core
{
    public class Converter
    {
        private MapMaker.Engine.Model.GameMaker.Manager Ruby { get; set; }
        private Dictionary<FileType, dynamic> RUBY_CACHE { get; set; }
        private Dictionary<FileType, IImport> IMPORT_CACHE { get; set; }
        private Dictionary<FileType, IEnumerable> VXACE_CACHE { get; set; }
        

        public Converter(string path)
        {
            Ruby = new MapMaker.Engine.Model.GameMaker.Manager();
            Ruby.Path = path;
            RUBY_CACHE = new Dictionary<FileType, dynamic>();
            IMPORT_CACHE = new Dictionary<FileType, IImport>();
            VXACE_CACHE = new Dictionary<FileType, IEnumerable>();
        }

        public void Import()
        {
            DateTime t = DateTime.Now;
            Import(FileType.Actors);
            Import(FileType.Animations);
            Import(FileType.Armors);
            Import(FileType.Classes);
            Import(FileType.CommonEvents);
            Import(FileType.Enemies);
            Import(FileType.Items);
            Import(FileType.Maps);
            Import(FileType.MapInfos);
            Import(FileType.Scripts);
            Import(FileType.Skills);
            Import(FileType.States);
            Import(FileType.System);
            Import(FileType.Tilesets);
            Import(FileType.Troops);
            Import(FileType.Weapons);
            Console.Write(DateTime.Now.Subtract(t).TotalSeconds);
            Convert();
        }

        private void Import(FileType type)
        {
            try
            {
                Ruby.Load(type, o => Import(type, o));
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("Ruby import error on file {0}. [{1}]", type, ex.Message));
            }
        }

        private void Import(FileType type, dynamic record)
        {
                RUBY_CACHE.Add(type, record);
                System.Diagnostics.Debug.WriteLine(string.Format("Imported file {0}", type));
        }

        private void Convert()
        {
            Convert<RPG_Actor>(FileType.Actors);
            Convert<RPG_Animation>(FileType.Animations);
            Convert<RPG_Armor>(FileType.Armors);
            Convert<RPG_Class>(FileType.Classes);
            Convert<RPG_CommonEvent>(FileType.CommonEvents);
            Convert<RPG_Enemy>(FileType.Enemies);
            Convert<RPG_Item>(FileType.Items);
            Convert<RPG_Map>(FileType.Maps);
            Convert<RPG_MapInfo>(FileType.MapInfos);
            //Convert<RPG_Sc(FileType.Scripts);
            Convert<RPG_Skill>(FileType.Skills);
            Convert<RPG_State>(FileType.States);
            Convert<RPG_System>(FileType.System);
            Convert<RPG_Tileset>(FileType.Tilesets);
            Convert<RPG_Troop>(FileType.Troops);
            Convert<RPG_Weapon>(FileType.Weapons);
        }

        private void Convert<T>(FileType type)
        {
            if (!RUBY_CACHE.ContainsKey(type)) return;
            if (!IMPORT_CACHE.ContainsKey(type))  {  IMPORT_CACHE.Add(type, new Manager<T>()); }
            IImport<T> mgr = (IImport<T>)IMPORT_CACHE[type];
            if(VXACE_CACHE.ContainsKey(type)) VXACE_CACHE.Remove(type);
            try
            {
                VXACE_CACHE.Add(type, mgr.Import(Ruby.Engine, RUBY_CACHE[type]));
                System.Diagnostics.Debug.WriteLine(string.Format("Conversion to C# complete on {0}", type));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Conversion to C# error on file {0}. [{1}]", type, ex.Message);
                System.Diagnostics.Debug.WriteLine(string.Format("Conversion to C# error on file {0}. [{1}]", type, ex.Message));
            }
            
        }
    }
}
